//
//  SGTabBarController.m
//  SemiGroup
//
//  Created by 陈无忌 on 12/9/15.
//  Copyright © 2015 陈无忌. All rights reserved.
//

#import "SGTabBarController.h"
#import "SGLoginViewController.h"

#define kCenterItemBorder 30

@interface SGTabBarController ()<UITabBarControllerDelegate>

@property (weak,nonatomic) UIVisualEffectView *backgroundview;
@property (weak,nonatomic) UIView *bottomView;
@property (weak,nonatomic) UIImageView *closeImageview;
@property (strong,nonatomic) NSMutableArray *menuviewsArray;

@end

@implementation SGTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置中间图片
    UIImage * midImage = [UIImage imageNamed:@"mid"];
    [self.tabBar.items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UITabBarItem * item = obj;
        if (idx == 2) {
            item.image = [midImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [midImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            //设置位置偏移
            item.imageInsets = UIEdgeInsetsMake(5.0, 0, -5.0, 0);
        }
    }];
    self.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//跳转登录
- (void)jumpToLogin{
    WS
    [UIAlertView showWithTitle:@"您还未登录" message:@"登录或注册以后才可以发起活动哦" cancelButtonTitle:nil otherButtonTitles:@[@"去登录",@"先不了"] tapBlock:^(UIAlertView * _Nonnull alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            UINavigationController *targetVC = [weakSelf.storyboard instantiateViewControllerWithIdentifier:@"SGUserWindowNavigationController"];
            [weakSelf presentViewController:targetVC animated:YES completion:nil];
        }
    }];
}

#pragma mark - UITabbar delegate

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [tabBar.items indexOfObject:item];
    if (index == 2) {
        UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView * blurView = [[UIVisualEffectView alloc] initWithEffect:blur];
        self.backgroundview = blurView;
        //            self.backgroundview.backgroundColor = UIColorFromRGB(0xf0eff5);
        [blurView setFrame:self.view.frame];
        [self.view addSubview:blurView];
        [UIView animateWithDuration:0.15 animations:^{
            
        } completion:^(BOOL finished) {
            [self showMenus];
        }];
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSArray * controllers = self.viewControllers;
    NSInteger index = [controllers indexOfObject:viewController];
    if (index == 2) {
        return NO;
    }
//    else if (index == 1 || index == 3 || index == 4){
//        if ([[SGUserManager sharedInstance] isNeedToJumpLogin]) {
//            [self jumpToLogin];
//            return NO;
//        }
//    }
    return YES;
}

#pragma mark - Private Method
- (void)showMenus{
    //动画
    //菜单动画
    NSArray * imagesNameArray = @[@"image1",@"image2",@"image3",@"image4",@"image5",@"image6"];
    NSArray * labelsArray = @[@"邮件",@"地图",@"信息",@"视频",@"新闻",@"天气"];
    CGFloat width = self.view.frame.size.width;
    CGFloat itemWidth = width/3;
    self.menuviewsArray = [NSMutableArray new];
    for (int index  = 0;index < 6;index ++) {
        NSTimeInterval delay = 0.03 * index;
        CGFloat centerx;
        CGFloat centery;
        //第一排
        if (index < 3) {
            centerx = width /6 * (2*index + 1);
            centery = self.view.frame.size.height - itemWidth - itemWidth - itemWidth/2 - 49;
        }else{
            centerx = width/6 * (2 *(index - 2) -1);
            centery = self.view.frame.size.height - itemWidth - itemWidth/2 - 49;
        }
        
        UIImage * image  = [UIImage imageNamed:imagesNameArray[index]];
        NSString * title = labelsArray[index];
        UIView * containview = [self createOneMenuViewWithImage:image Title:title];
        containview.center = CGPointMake(centerx, self.view.frame.size.height);
        UITapGestureRecognizer * menuTab = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(catchMenuTap:)];
        [containview addGestureRecognizer:menuTab];
        [self.view addSubview:containview];
        [self.menuviewsArray addObject:containview];
        containview.tag = index;
        [UIView animateWithDuration:0.15
                              delay:delay
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             containview.center = CGPointMake(centerx, centery - 20);
                         }
                         completion:^(BOOL finished) {
                             [UIView animateWithDuration:0.05
                                                   delay:0
                                                 options:UIViewAnimationOptionCurveEaseInOut
                                              animations:^{
                                                  containview.center = CGPointMake(centerx, centery);
                                              }
                                              completion:^(BOOL finished) {
                                                  
                                              }];
                         }];
    }
    //底部view
    UIView * bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, kScreenWidth, 49)];
    bottomView.backgroundColor = UIColorFromRGB(0xd8d7de);
    //关闭按钮
    UIImageView * closeImageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kCenterItemBorder, kCenterItemBorder)];
    [bottomView addSubview:closeImageview];
    closeImageview.image = [UIImage imageNamed:@"mid"];
    closeImageview.center = CGPointMake(CGRectGetWidth(bottomView.bounds)/2, CGRectGetHeight(bottomView.bounds)/2);
    self.closeImageview = closeImageview;
    //添加手势
    UITapGestureRecognizer * closeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMenus)];
    closeImageview.userInteractionEnabled = true;
    [closeImageview addGestureRecognizer:closeTap];
    [self.view addSubview:bottomView];
    self.bottomView = bottomView;
    
    [UIView animateWithDuration:0.4
                     animations:^{
                         closeImageview.transform = CGAffineTransformMakeRotation(M_PI/4);
                     }
                     completion:^(BOOL finished) {
                         
                     }];
}

- (UIView *)createOneMenuViewWithImage:(UIImage *)image Title:(NSString *)title{
    CGFloat width = self.view.frame.size.width;
    CGFloat itemWidth = width/3;
    UIView * containview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, itemWidth, itemWidth)];
    UIImageView * iconImageview = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,itemWidth - 40, itemWidth - 40)];
    
    iconImageview.center = CGPointMake(itemWidth/2, itemWidth/2);
    iconImageview.contentMode = UIViewContentModeScaleAspectFill;
    iconImageview.image = image;
    [containview addSubview:iconImageview];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,itemWidth-20,itemWidth, 20)];
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = title;
    [containview addSubview:label];
    
    return containview;
}


- (void)hideMenus{
    //隐藏菜单栏
    
    for (NSInteger index = 5; index >= 0; index--) {
        UIView * containview = self.menuviewsArray[index];
        CGFloat  dealy = (5 - index)*0.03;
        [UIView animateWithDuration:0.2
                              delay:dealy
                            options:UIViewAnimationOptionCurveLinear
                         animations:^{
                             containview.center = CGPointMake(containview.center.x, self.view.frame.size.height + CGRectGetWidth(containview.frame)/2);
                         }
                         completion:^(BOOL finished) {
                             [containview removeFromSuperview];
                         }];
    }
    [UIView animateWithDuration:0.4
                     animations:^{
                         self.closeImageview.transform = CGAffineTransformIdentity;
                         self.bottomView.alpha = 0.0;
                         self.backgroundview.alpha = 0.0;
                     }
                     completion:^(BOOL finished) {
                         [self.bottomView removeFromSuperview];
                         [self.backgroundview removeFromSuperview];
                     }];
    
    self.menuviewsArray = nil;
}

-(void)catchMenuTap:(UITapGestureRecognizer *)tap{
    if ([[SGUserManager sharedInstance] isNeedToJumpLogin]) {
        [self jumpToLogin];
        return;
    }
    NSLog(@"Catch tap:%ld",(long)tap.view.tag);
    [self hideMenus];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
